var rec_e_inv_ind='N';//是否需要电子发票
var cont_bef_del="N";//是否在发货前联系你
var order_cmt='';//订单评论
var hamper_msg='';
var del_win_per='';//窗口支付时间段
var del_date='';//选择的日期
var pay_mtd='CASH';//支付方式
var address_Name='';
var address_key='';
$(function() {

    judgeLogin();//判断是否登录
})

//判断是否登录
function judgeLogin(){
    var arr={
        hf_s_id:"WD11",
        hf_srv_id:"Zv_jcus_is_login"
    };
    getAjaxRequest(arr,function(result) {
        if(result.bs_is_login==BS_IS_LOGIN.Y){
            $('.selectCate.hasLogin').show();
            $('.selectCate.noLogin').hide();
            getShopCarList();//获取购物车列表
            getAddressList();//获取地址列表
            getEcouponList();//获取优惠列表

        }else{
            var redirectUrl = encodeURI(location.href);
            location.href = MPJ.base + "/pages/login.jsp?redirectUrl="+redirectUrl;
            //location.href=MPJ.base+"/pages/login.jsp";
        }
    })
}

//获取购物车列表
function getShopCarList(){
    var arr={
        hf_s_id:"WD11",
        hf_srv_id:"Av_jcart",
    };
    getAjaxRequest(arr,function(result) {
        if (result.bs_is_login == BS_IS_LOGIN.Y) {
            $('.totalOrder span.mui-pull-right').html('$' + result.bj_total_item_amount);//设置购物车中商品总数
            var BvcCcartIitem = result.bvc_cart_item;
            var carList = '';
            if (BvcCcartIitem.length != 0) {
                for (var i = 0; i < BvcCcartIitem.length; i++) {
                    carList += '<div class="item" data-id="' + BvcCcartIitem[i].bj_pdt_id + '" data-item-id="' + BvcCcartIitem[i].bs_item_no + '">' +
                        '<div class="proImg">' +
                        '<img src="' + MPJ.apiBase + '' + BvcCcartIitem[i].bs_simg_file + '"/>' +
                        '</div>' +
                        '<div class="mui-media-body">' +
                        '<div class="pro-name">' + BvcCcartIitem[i].bs_brand + ' </div>' +
                        '<div class="pro-f-name">' + BvcCcartIitem[i].bs_desc + '</div>' +

                        '<div class="price-font">' +
                        '<div class="orange-font">' + BvcCcartIitem[i].bs_prom_text + '</div>' +
                        '<div class="price">$' + BvcCcartIitem[i].bj_price_inuse + ' </div>' +
                        '</div>' +
                        '</div>' +
                        '<div class="deleteBtn">' +
                        '<img src="' + MPJ.base + '/resources/images/icon/delete-icon.png" />' +
                        '</div>' +
                        '</div>';
                }

            }
            $('.yourOrder .orderList').html(carList);

            $('.orderPayment p em').html(result.bj_total_item_count);
            $('.orderPayment p span').html("$" + result.bj_total_item_amount);
            OperShoppingCart();//操作购物车函数
        } else {
            var redirectUrl = encodeURI(location.href);
            location.href = MPJ.base + "/pages/login.jsp?redirectUrl="+redirectUrl;
        }
    })
}

//操作购物车函数函数
function  OperShoppingCart(){
    $('.yourOrder .orderList .item').each(function(index){
        var $this=$(this);
        var id=$(this).data("id");//获取商品bj_pdt_id
        var item_id=$(this).data("item_id");//获取bs_item_no
        //跳转到商品详情页
        $this.on('tap','.proImg',function(){
            var pdt_id=$this.data('id');
            var item_no=$this.data('item-no');
            var product_info={};
            product_info.pdt_id=pdt_id;
            product_info.item_no=item_no;
            sessionStorage.setItem('product_info',JSON.stringify(product_info));
            location.href=MPJ.base+"/pages/product/product-detail.jsp";
        })

        //删除购物车
        var $btn=$this.find('.deleteBtn')
        $btn.on('tap',function(){
            var arr={
                hf_s_id:"WD11",
                hf_srv_id:"Av_jcart",
                hs_action_id:"delete_from_cart",
                hs_buy_pdt_id:id,
                hvs_cart_pdt_id:id,
                hs_del_pdt_pos:0,
            };
            getAjaxRequest(arr,function(result){
                if (result.bs_result=BS_RESULT.OK){
                    getShopCarList();
                }
            });
        })
    })
}

//获取用户基本信息
function getUserBaseInfo(){
    var arr={
        hf_s_id:"WD11",
        hs_set_new_order_addr_key:address_key,
        hf_srv_id:"Av_jcheckout",
        hs_action_id:''
    };
    getAjaxRequest(arr,function(result) {
        if (result.bs_result == BS_RESULT.OK) {
            $('.selectCate.hasLogin #h_name').val(result.bs_first_name);
            $('.selectCate.hasLogin #h_surname').val(result.bs_family_name);
            $('.selectCate.hasLogin span.h_address').html(result.bs_addr_name);
            $('.selectCate.hasLogin #h_phone').val(result.bs_main_phone);
            address_Name = result.bs_addr_name;
            if (result.bs_address != "") {
                $('.selectCate.hasLogin .h_address').html(result.h_address);
                $('.pick-up-location .h_address').val(result.h_address);
            }

            if (result.bs_cont_bef_del == "N") {
                cont_bef_del = "N";
                $('.edit-info .checkBox').removeClass('active');
                $('.edit-info .checkBox span img').attr('src', MPJ.base + "/resources/images/icon/delivery-no-select.png");
            } else {
                cont_bef_del = "Y";
                $('.edit-info .checkBox').addClass('active');
                $('.edit-info .checkBox span img').attr('src', MPJ.base + "/resources/images/icon/delivery-select.png");
            }

            var bvc_cc_list = '';
            if (result.bvc_cc_list.length != 0) {
                for (var i = 0; i < result.bvc_cc_list.length; i++) {
                    bvc_cc_list += '<option value="' + result.bvc_cc_list[i].bj_cc_key + '">' + result.bvc_cc_list[i].bs_cc_mask_no + '</option>';
                }
            }
            $('.delivery-address span.h_address').html(result.bs_address);
            order_cmt = result.bs_order_cmt;
            hamper_msg = result.bs_hamper_msg;

            $('#selet-card').html(bvc_cc_list);
            getCalendar(result.bc_del_win);

        }
    })
}

//获取地址列表
function getAddressList(){
    var arr={
        hf_s_id:"WD11",
        hf_srv_id:"Cv_jcus_address",
        hs_action_id:"get_full_list"
    };
    getNoResultAjaxRequest(arr,function(result) {
        var address_str='';
        for(var i=0;i<result.length;i++){
            if(result[i].bs_default_ind=="Y"){
                address_key=result[i].bj_addr_key;
                getUserBaseInfo(); //获取用户的基本信息
                address_str+='<option value="'+result[i].bj_addr_key+'" selected="selected">'+result[i].bs_addr_name+'</option>';
            }else{
                address_str+='<option value="'+result[i].bj_addr_key+'">'+result[i].bs_addr_name+'</option>';
            }
        }
        $('.hasLogin #pickerBox').html(address_str);

    })
}

//收货地址改变
$('.hasLogin #pickerBox').change(function () {
    dialog('Select Pick-up Location', 'You previously selected Delivery Service. If you change the delivery method, some products may become unavailable. Would you like to proceed?', 3);
    var $dialog = $('#dialog');
    $dialog.show();
    $dialog.find('.comfirmBtn').one('click', function() {
        $dialog.hide();
        $dialog.remove();
        //$('.delivery-address span.h_address').html($('.hasLogin #pickerBox').val());
        address_key=$('.hasLogin #pickerBox option:selected').val();
        getUserBaseInfo();
    });
})

//是否送货前联系你
$('.edit-info .checkBox').on('tap',function(){
    if($(this).hasClass('active')){
        cont_bef_del="N";
        $(this).removeClass('active');
        $(this).find('span img').attr('src',MPJ.base+"/resources/images/icon/delivery-no-select.png");
    }else{
        cont_bef_del="Y";
        $(this).addClass('active');
        $(this).find('span img').attr('src',MPJ.base+"/resources/images/icon/delivery-select.png");
    }
})

//添加收货地址
$('.main-info a.mui-btn').on('tap', function() {
    location.href=MPJ.base+"/pages/account/receiver/add.jsp";
})

//商品列表的收起与隐藏
$('.yourOrder h1').on('tap', function() {
    var $listBox = $('.yourOrder .orderList');
    var $img = $('.yourOrder h1 span img');
    if ($listBox.css('display') == "none") {
        $listBox.slideDown();
        $img.attr('src', MPJ.base+'/resources/images/icon/delivery-jt-up.png');
    } else {
        $listBox.slideUp();
        $img.attr('src', MPJ.base+'/resources/images/icon/delivery-jt-down.png');
    }
})

//文本域的收取与隐藏
$('.edit-info h4').on('tap', function() {
    var $listBox = $('.edit-info .textarea');
    var $img = $('.edit-info h4 span img');
    if ($listBox.css('display') == "none") {
        $listBox.slideDown();
        $img.attr('src', MPJ.base+'/resources/images/icon/delivery-jt-up.png');

    } else {
        $listBox.slideUp();
        $img.attr('src', MPJ.base+'/resources/images/icon/delivery-jt-down.png');

    }
})


//弹出框的显示与隐藏
$('.btnBox .payMent').on('tap', function() {
    $('#screen').show();
})
$('.alert-title span').on('tap', function() {
    $('#screen').hide();
})
//配送方式选择
$('.hasLogin #h_method').change(function(){
    var str=$('#h_method').val();
    if (str == "Delivery") {
        $('.hasLogin').removeClass('on');
    }else if (str == "Click and Collect") {
        var district_Arr=JSON.parse(sessionStorage.getItem('district_Arr'));
        for(var i=0;i<district_Arr.length;i++){
            var district=district_Arr[i].district;
            for(var j=0;j<district.length;j++){
                if(district[j].id == "SHOP380"){
                    var cnc_ind=district[j].cnc_ind;
                    for(var k=0;k<cnc_ind.length;k++){
                        if(cnc_ind[k].id=="Y"){
                            var cnc_str='<option value="'+district[j].id+'">'+district[j].name+'</option>';
                            dialog('Select Pick-up Location', '<select id="alert_address" class="select">'+cnc_str+'</select>', 1);
                            var $dialog = $('#dialog');
                            $dialog.show();
                            $('.weui_dialog_ft').addClass('setWidth');
                            $dialog.find('.comfirmBtn').one('click', function() {
                                $('.hasLogin #h_address').val($('#alert_address option:selected').text());
                                $dialog.hide();
                                $dialog.remove();

                                $('.hasLogin').addClass('on');

                                judgeLogin();//判断是否登录
                            });
                        }
                    }
                    break;
                }

            }

        }
    }
})

//获取优惠列表
function getEcouponList(){
    var arr={
        hf_s_id: 'WD11',
        hf_srv_id: 'Av_jcoupon_wallet',
        hs_check_cart_value: 'Y',
        hs_call_ts:''
    };
    getAjaxRequest(arr,function(result) {
        var ecoupon_str='';
        var bvc_coupon_item=result.bvc_coupon_item;
        if(bvc_coupon_item.length>0){
            for(var i=0;i<bvc_coupon_item.length;i++){
                ecoupon_str+='<div class="item" data-id="'+bvc_coupon_item[i].bj_coupon_id+'" data-type="'+bvc_coupon_item[i].bs_coupon_type+'">'+
                    '<h4>'+bvc_coupon_item[i].bs_prom_text+'</h4>'+
                    '<span>Terms & Conditions</span>'+
                    '<p> Expiry Date: '+bvc_coupon_item[i].bs_end_dt+'</p>'+
                    '<a class="mui-btn" >Redeem Now</a>'+
                    '</div>';
            }
            $('.coupon .list').html(ecoupon_str);


            $('.coupon .list .item').each(function(){
                var $this=$(this);
                var $ramdom_now=$this.find('a.mui-btn');
                $ramdom_now.on('tap',function(){
                    if($(this).hasClass('active')){
                        $(this).removeClass("active");
                    }else{
                        $(this).addClass("active");
                    }
                })
            })
        }
    })
}